25#include "yaml-cpp/yaml.h"
51 using map<char, T>::map;
53 const std::string& _field,
55 const std::string& _keys =
ALL_KEYS);
58 bool has(
char _key)
const;
64 for (
auto&& pair : _x)
66 _os << pair.first <<
": " << pair.second << std::endl;
80 const std::string& _field,
82 const std::string& _keys)
88 throw std::runtime_error(
"Composite: constructor with a non-map yaml node");
93 for (
auto key : _keys)
94 if (not _n[key] and _required)
96 throw std::runtime_error(std::string(
"Composite ") +
typeid(T).name() +
97 " constructor: Missing node for key '" + key +
"'.");
101 for (
auto node_pair : _n)
107 key = node_pair.first.as<
char>();
109 catch (
const std::exception& e)
111 throw std::runtime_error(std::string(
"In Composite ") +
typeid(T).name() +
112 " constructor: There is a key of type different than char: " +
113 node_pair.first.as<std::string>() +
". Error: " + e.what());
118 _keys.find(key) == std::string::npos)
122 if (not _field.empty())
124 if (node_pair.second[_field])
128 this->emplace(key, node_pair.second[_field].as<T>());
130 catch (
const std::exception& e)
133 em << node_pair.second[_field];
134 throw std::runtime_error(
"In Composite constructor: Failed to emplace the entry " + _field +
135 " for key '" + key +
"' with error: " + e.what() +
"\nNode:\n" +
142 em << node_pair.second;
143 throw std::runtime_error(
"In Composite constructor: Missing mandatory field '" + _field +
144 "' for key '" + key +
"'\nNode:\n" + em.c_str());
151 this->emplace(key, node_pair.second.as<T>());
153 catch (
const std::exception& e)
156 em << node_pair.second;
157 throw std::runtime_error(
"In Composite constructor: Failed to emplace the entry for key '" +
158 std::string(1, key) +
"' with error: " + e.what() +
"\nNode:\n" +
166 WOLF_WARN(
"The YAML node is not defined. Constructing an empty composite");
173 return this->count(_key) != 0;
179 for (
auto&& key : _query_keys)
193 for (
auto&& pair : *
this) keys.push_back(pair.first);
201 if (not this->has(_keys))
203 throw std::runtime_error(
"Composite<T>::operator() required keys " + _keys +
" are not available, only have " +
208 for (
auto key : _keys)
210 output.emplace(key, this->at(key));
218 return YAML::Node(*
this);
227struct convert<
wolf::Composite<T>>
232 for (
auto&& pair : composite)
234 n[pair.first] = pair.second;
Definition: composite.h:47
Composite(const YAML::Node &_n, const std::string &_field, const bool _required, const std::string &_keys=ALL_KEYS)
Definition: composite.h:79
StateKeys getKeys() const
Definition: composite.h:190
bool has(const StateKeys &_structure) const
Definition: composite.h:177
T CompositeType
Definition: composite.h:49
virtual ~Composite()=default
Composite operator()(const std::string &_keys) const
Definition: composite.h:199
friend std::ostream & operator<<(std::ostream &_os, const Composite< T > &_x)
Definition: composite.h:62
YAML::Node toYaml() const
Definition: composite.h:216
bool has(char _key) const
Definition: composite.h:171
#define WOLF_WARN(...)
Definition: logging.h:436
Definition: composite.h:225
Definition: capture_base.h:23
std::string StateKeys
Definition: composite.h:31
static std::string ALL_KEYS
Definition: composite.h:33
Composite< bool > BoolComposite
Definition: composite.h:76
StateKeys keysIntersection(const StateKeys &_keys1, const StateKeys &_keys2)
Intersection of two sets of keys: the keys of _keys1 that are also in _keys2, in the order of _keys1 ...
Definition: composite.cpp:31
StateKeys keysUnion(const StateKeys &_keys1, const StateKeys &_keys2)
Union of two sets of keys: the keys of _keys1 followed by the keys of _keys2 not in _keys1 (no duplic...
Definition: composite.cpp:23
static Node encode(const wolf::Composite< T > &composite)
Definition: composite.h:229
static bool decode(const Node &node, wolf::Composite< T > &composite)
Definition: composite.h:240